package com.stjywl.report;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.validator.constraints.SafeHtml;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

//import com.jeesite.modules.jy.storage.entity.JyStProductbarcode;
import com.xml.util.ServerUtility;

import gridreport.ExportType;
import gridreport.test.DataTextProvider;

/**
 * 锐狼报表打印【web插件打印，设计打印模板】Controller
 * @author zdy
 * @version 2020-05-27
 */
@Controller
@RequestMapping(value = "/report")
public class RLReportUnitController {
	 // /ys/report/serverReport
	 @RequestMapping(value = "serverReport")
	 public void serverReport(HttpServletRequest request,HttpServletResponse response) throws Exception {
//		 String reportPath = "C:\\Grid++Report 6\\WebSamples\\jsp\\grf\\";
//		 String dataPath = "C:\\Grid++Report 6\\WebSamples\\data\\json\\";
//		 String grfFileName = "1o.grf";
//		 String dataFileName = "jsonCategories.txt";
		 
		 try {
			//创建报表对象
			gridreport.Report report =new gridreport.Report(); 

			//从文件载入报表模板
			String FileName = request.getRealPath("grf")+ "/jyClothSaSaleorder.grf";
			Boolean Success = report.LoadFromFile(FileName); 
		    if (!Success) {
				System.out.println("载入报表模板失败，文件：" + FileName);
		        return;
		    }

		    //载入报表数据，为约定格式的 XML 或 JSON 文本数据,设置查询接口
		    String reportDataText = "{\r\n" + 
		    		"	\"jySaSaleorderBList\": [\r\n" + 
		    		"		{\r\n" + 
		    		"			\"isNewRecord\": false,\r\n" + 
		    		"			\"id\": \"1317304599726792704\",\r\n" + 
		    		"			\"remarks\": \"\",\r\n" + 
		    		"			\"idb\": \"1317304599726792704\",\r\n" + 
		    		"			\"idwarehouse\": \"1313299510711574528\",\r\n" + 
		    		"			\"idinventory\": \"1306497686730752000\",\r\n" + 
		    		"			\"color\": \"胚纱\",\r\n" + 
		    		"			\"quantity\": 2.0,\r\n" + 
		    		"			\"weight\": 11.0,\r\n" + 
		    		"			\"discountrate\": 100.0,\r\n" + 
		    		"			\"price\": 12.0,\r\n" + 
		    		"			\"retailprice\": 12.0,\r\n" + 
		    		"			\"taxrate\": 0.0,\r\n" + 
		    		"			\"taxprice\": 12.0,\r\n" + 
		    		"			\"discountamount\": 24.0,\r\n" + 
		    		"			\"taxamount\": 24.0,\r\n" + 
		    		"			\"isrefclose\": \"0\",\r\n" + 
		    		"			\"ispresent\": \"0\",\r\n" + 
		    		"			\"executedQty\": 0.0,\r\n" + 
		    		"			\"executableQty\": 2.0,\r\n" + 
		    		"			\"isRefMomClose\": \"0\",\r\n" + 
		    		"			\"ischoseClose\": \"0\",\r\n" + 
		    		"			\"busiCode\": \"15\",\r\n" + 
		    		"			\"busiName\": \"普通销售\",\r\n" + 
		    		"			\"partnerCodeb\": \"003\",\r\n" + 
		    		"			\"partnerNameb\": \"有有\",\r\n" + 
		    		"			\"code\": \"SO2020100002\",\r\n" + 
		    		"			\"voucherdate\": \"2020-10-17\",\r\n" + 
		    		"			\"idpartner\": \"1305707503076536320\",\r\n" + 
		    		"			\"businesstype\": \"1275255010689302528\",\r\n" + 
		    		"			\"externalCoding\": \"0084C#\",\r\n" + 
		    		"			\"invCodeb\": \"0084C#\",\r\n" + 
		    		"			\"invNameb\": \"30s/1*2双纱丝光棉罗纹1*1密根（上下食筋）\",\r\n" + 
		    		"			\"unitName\": \"匹\"\r\n" + 
		    		"		},\r\n" + 
		    		"		{\r\n" + 
		    		"			\"isNewRecord\": false,\r\n" + 
		    		"			\"id\": \"1317304599726792704\",\r\n" + 
		    		"			\"remarks\": \"\",\r\n" + 
		    		"			\"idb\": \"1317304599726792704\",\r\n" + 
		    		"			\"idwarehouse\": \"1313299510711574528\",\r\n" + 
		    		"			\"idinventory\": \"1306497686730752000\",\r\n" + 
		    		"			\"color\": \"胚纱\",\r\n" + 
		    		"			\"quantity\": 2.0,\r\n" + 
		    		"			\"weight\": 11.0,\r\n" + 
		    		"			\"discountrate\": 100.0,\r\n" + 
		    		"			\"price\": 12.0,\r\n" + 
		    		"			\"retailprice\": 12.0,\r\n" + 
		    		"			\"taxrate\": 0.0,\r\n" + 
		    		"			\"taxprice\": 12.0,\r\n" + 
		    		"			\"discountamount\": 24.0,\r\n" + 
		    		"			\"taxamount\": 24.0,\r\n" + 
		    		"			\"isrefclose\": \"0\",\r\n" + 
		    		"			\"ispresent\": \"0\",\r\n" + 
		    		"			\"executedQty\": 0.0,\r\n" + 
		    		"			\"executableQty\": 2.0,\r\n" + 
		    		"			\"isRefMomClose\": \"0\",\r\n" + 
		    		"			\"ischoseClose\": \"0\",\r\n" + 
		    		"			\"busiCode\": \"15\",\r\n" + 
		    		"			\"busiName\": \"普通销售\",\r\n" + 
		    		"			\"partnerCodeb\": \"003\",\r\n" + 
		    		"			\"partnerNameb\": \"有有\",\r\n" + 
		    		"			\"code\": \"SO2020100002\",\r\n" + 
		    		"			\"voucherdate\": \"2020-10-17\",\r\n" + 
		    		"			\"idpartner\": \"1305707503076536320\",\r\n" + 
		    		"			\"businesstype\": \"1275255010689302528\",\r\n" + 
		    		"			\"externalCoding\": \"0084C#\",\r\n" + 
		    		"			\"invCodeb\": \"0084C#\",\r\n" + 
		    		"			\"invNameb\": \"双面涤棉健康布珠地拉架\",\r\n" + 
		    		"			\"unitName\": \"匹\"\r\n" + 
		    		"		},\r\n" + 
		    		"		{\r\n" + 
		    		"			\"isNewRecord\": false,\r\n" + 
		    		"			\"id\": \"1317304599726792704\",\r\n" + 
		    		"			\"remarks\": \"\",\r\n" + 
		    		"			\"idb\": \"1317304599726792704\",\r\n" + 
		    		"			\"idwarehouse\": \"1313299510711574528\",\r\n" + 
		    		"			\"idinventory\": \"1306497686730752000\",\r\n" + 
		    		"			\"color\": \"胚纱\",\r\n" + 
		    		"			\"quantity\": 2.0,\r\n" + 
		    		"			\"weight\": 11.0,\r\n" + 
		    		"			\"discountrate\": 100.0,\r\n" + 
		    		"			\"price\": 12.0,\r\n" + 
		    		"			\"retailprice\": 12.0,\r\n" + 
		    		"			\"taxrate\": 0.0,\r\n" + 
		    		"			\"taxprice\": 12.0,\r\n" + 
		    		"			\"discountamount\": 24.0,\r\n" + 
		    		"			\"taxamount\": 24.0,\r\n" + 
		    		"			\"isrefclose\": \"0\",\r\n" + 
		    		"			\"ispresent\": \"0\",\r\n" + 
		    		"			\"executedQty\": 0.0,\r\n" + 
		    		"			\"executableQty\": 2.0,\r\n" + 
		    		"			\"isRefMomClose\": \"0\",\r\n" + 
		    		"			\"ischoseClose\": \"0\",\r\n" + 
		    		"			\"busiCode\": \"15\",\r\n" + 
		    		"			\"busiName\": \"普通销售\",\r\n" + 
		    		"			\"partnerCodeb\": \"003\",\r\n" + 
		    		"			\"partnerNameb\": \"有有\",\r\n" + 
		    		"			\"code\": \"SO2020100002\",\r\n" + 
		    		"			\"voucherdate\": \"2020-10-17\",\r\n" + 
		    		"			\"idpartner\": \"1305707503076536320\",\r\n" + 
		    		"			\"businesstype\": \"1275255010689302528\",\r\n" + 
		    		"			\"externalCoding\": \"0084C#\",\r\n" + 
		    		"			\"invCodeb\": \"0084C#\",\r\n" + 
		    		"			\"invNameb\": \"双面涤棉健康布珠地拉架\",\r\n" + 
		    		"			\"unitName\": \"匹\"\r\n" + 
		    		"		}\r\n" + 
		    		"	],\r\n" + 
		    		"	\"jyClothSaSaleordersList\": [\r\n" + 
		    		"		{\r\n" + 
		    		"			\"isNewRecord\": false,\r\n" + 
		    		"			\"id\": \"1317291466312986624\",\r\n" + 
		    		"			\"createBy\": \"system\",\r\n" + 
		    		"			\"createDate\": \"2020-10-17 10:28\",\r\n" + 
		    		"			\"remarks\": \"\",\r\n" + 
		    		"			\"updateDate\": \"2020-10-19 16:46\",\r\n" + 
		    		"			\"status\": \"0\",\r\n" + 
		    		"			\"updateBy\": \"system\",\r\n" + 
		    		"			\"voucherdate\": \"2020-10-17\",\r\n" + 
		    		"			\"code\": \"SO2020100002\",\r\n" + 
		    		"			\"idpartner\": \"1305707503076536320\",\r\n" + 
		    		"			\"idclerk\": \"\",\r\n" + 
		    		"			\"businesstype\": \"1275255010689302528\",\r\n" + 
		    		"			\"itemtype\": \"\",\r\n" + 
		    		"			\"address\": \"\",\r\n" + 
		    		"			\"recivetype\": \"4\",\r\n" + 
		    		"			\"totalquantity\": 2.0,\r\n" + 
		    		"			\"idwarehouse\": \"\",\r\n" + 
		    		"			\"auditor\": \"超级管理员\",\r\n" + 
		    		"			\"auditeddate\": \"2020-10-19 16:46:51\",\r\n" + 
		    		"			\"auditstatus\": \"1\",\r\n" + 
		    		"			\"isrefclose\": \"0\",\r\n" + 
		    		"			\"refsourceorder\": \"\",\r\n" + 
		    		"			\"refsourceorderid\": \"\",\r\n" + 
		    		"			\"refsource\": \"\",\r\n" + 
		    		"			\"partnerCodeb\": \"003\",\r\n" + 
		    		"			\"partnerNameb\": \"有有\",\r\n" + 
		    		"			\"busiCode\": \"15\",\r\n" + 
		    		"			\"busiName\": \"普通销售\",\r\n" + 
		    		"			\"userCode2\": \"system\",\r\n" + 
		    		"			\"userName2\": \"超级管理员\",\r\n" + 
		    		"			\"userCode3\": \"system\",\r\n" + 
		    		"			\"userName3\": \"超级管理员\",\r\n" + 
		    		"			\"isRefMomClose\": \"0\",\r\n" + 
		    		"			\"ischoseClose\": \"0\"\r\n" + 
		    		"		}\r\n" + 
		    		"	]\r\n" + 
		    		"}";
		    //String reportDataText = DataTextProvider.Customer();
		    //Success =  report.LoadDataFromURL(reportDataText);
		    Success = report.LoadDataFromXML(reportDataText);
		    if (!Success) {
		    	System.out.println("载入报表数据失败，数据文本：" + reportDataText);
		        return;
		    }

		    gridreport.BinaryObject bo = report.ExportDirectToBinaryObject(ExportType.PDF);
		    
		    //将生成的数据响应给客户端
		    ServerUtility.ResponseBinary(request, response, bo, "YourFileName.pdf", "application/pdf", "attachment"); //attachment ： inline
		    
//			//设置打印相关的参数
//			String printerName = "HP Officejet 4500 G510a-f"; //"Foxit Reader PDF Printer";
//			gridreport.Printer printer = report.getPrinter();
//			printer.setPrinterName(printerName);
//
//			//执行打印任务
//			report.Print(false);

		    //发送一个消息文本给客户端，这个不是必须的
//			ResponseText(response, "在服务器端成功执行打印输出。");
		}
		catch(Exception e) {
		    //out.println(e.toString());
			 ServerUtility.ResponseException(response, e.toString());
		}
	 }
}
